package com.zilla.android.zillacore.libzilla.ui;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import com.github.snowdream.android.util.Log;
import com.squareup.picasso.Picasso;
import com.zilla.android.zillacore.libzilla.listener.OnGetEditTextValuesListener;
import com.zilla.android.zillacore.libzilla.util.ReflectUtil;

import java.io.File;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;

/**
 * Created by zilla on 14-7-14.
 */
public class AdapterUtil<T> {

    private Context context;

    int index = 0;

    public AdapterUtil(Context context) {
        this.context = context;
    }


    HashMap<Integer,String> hashMap = new HashMap<Integer, String>();

    public View getView(List<T> list, LayoutInflater inflater,
                        int resid, Class<?> viewHolder,
                        CompoundButton.OnCheckedChangeListener onCheckedChangeListener,
                        final int position, View convertView, ViewGroup parent) {
        T item = list.get(position);
        Object holder;
        if (convertView != null) {
            holder = convertView.getTag();
        } else {
            convertView = inflater.inflate(resid, parent, false);
            holder = getConstructorInstance(viewHolder, convertView);
            if (holder == null) {
                return convertView;
            }
            convertView.setTag(holder);
        }
        Field[] fields = viewHolder.getDeclaredFields();
        for (Field field : fields) {
            Class<?> dataType = field.getType();
            //首先判断item中是否存在该字段
            Object modelProperty = "";
            try {
                modelProperty = ReflectUtil.getObj(item, field.getName());
            } catch (Exception e) {
                Log.e(e.getMessage());
                //如果item中不存在该字段，跳过
                continue;
            }
//            if (TextUtils.isEmpty(modelProperty)) {
//                continue;
//            }
            field.setAccessible(true);

            try {
                View view = (View) field.get(holder);
                view.setTag(item);
            } catch (Exception e) {
                Log.e(e.getMessage());
            }
            //TextView
            if (dataType == TextView.class) {
                try {
                    ((TextView) field.get(holder)).setText((String) modelProperty);
                } catch (Exception e) {
                    Log.e(e.getMessage());
                }
            }
            //Button
            else if (dataType == Button.class) {
                try {
                    ((Button) field.get(holder)).setText((String) modelProperty);
                } catch (IllegalAccessException e) {
                    Log.e(e.getMessage());
                }
            }
            //CheckBox
            else if (dataType == CheckBox.class) {
                try {
                    boolean isChecked = false;
                    if ("1".equals(modelProperty)) {
                        isChecked = true;
                    } else {
                        isChecked = false;
                    }
                    CheckBox checkBox = ((CheckBox) field.get(holder));
                    checkBox.setChecked(isChecked);
                    if (onCheckedChangeListener != null) {
                        checkBox.setOnCheckedChangeListener(onCheckedChangeListener);
                    }
                } catch (Exception e) {
                    Log.e(e.getMessage());
                }
            }
            //ImageView
            else if (dataType == ImageView.class) {
                try {
                    android.widget.ImageView img = (android.widget.ImageView) field.get(holder);
                    if (modelProperty.getClass() == String.class) {
                        if (((String) modelProperty).startsWith("http")) {//url
                            Picasso.with(context).load(((String) modelProperty)).into(img);
                        } else if (((String) modelProperty).startsWith("/")) {//file
                            Picasso.with(context).load(new File(((String) modelProperty))).into(img);
                        }
                    } else {
                        try {
                            int res = (Integer) modelProperty;
                            Picasso.with(context).load(res).into(img);
                        } catch (Exception e) {
                            Log.e(e.getMessage());
                        }
                    }
                } catch (Exception e) {
                    Log.e(e.getMessage());
                }
            } else if (dataType == RatingBar.class) {
                try {
                    ((RatingBar) field.get(holder)).setRating((Integer) modelProperty);
                } catch (Exception e) {
                    Log.e(e.getMessage());
                }
            }

            //其它
        }
        return convertView;
    }

    protected Object getConstructorInstance(Class c, View view) {
        try {
            Constructor ctor = c.getDeclaredConstructor(View.class);
            return ctor.newInstance(view);
        } catch (Exception e) {
            Log.e(e.getMessage());
            Log.e("==Error:getConstructorInstance");
        }
        return null;
    }


}
